<?php

require_once  __DIR__.'/vendor/autoload.php';

use think\Db;
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
//use app\common\model\config\BasicsConfigData;
use think\admin\Exception;

/*if (isset($argv[2])){
    $ucode = $argv[2];
}else{
    throw new Exception("用户编码不存在");
}

$ukeyWs = BasicsConfigData::mk()->where(['ucode' => $ucode,'type' => 'basics','group' => 'company','code' => 'ukey_ws'])->value('value');
$ukeyWsPassword = BasicsConfigData::where('basics_config_data')->where(['ucode' => $ucode,'type' => 'basics','group' => 'company','code' => 'ukey_ws_password'])->value('value');

if (empty($ukeyWs) || empty($ukeyWsPassword)) {
    throw new Exception("ukey服务信息配置不完整");
}*/

//$list = \think\admin\model\SystemUser::where('id', '>', 0)->find();

$worker = new Worker();


$worker->onWorkerStart = function ($worker) {

    //'ws://192.168.1.30:61232'
    $con = new AsyncTcpConnection('ws://192.168.1.30:61232');
    $con->websocketPingInterval = 30;
    // websocket握手成功后
    $con->onWebSocketConnect = function (AsyncTcpConnection $con) {
        echo "握手";
        $data = [
            '_method' => 'cus-sec_SpcVerifyPINAll',
            '_id' => 0,
            'args' => [
                'passwd' => '88888888'
            ]
        ];
        $con->send(json_encode($data,320));
    };
    // 连接上发生错误时，一般是连接远程websocket服务器失败错误
    $con->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
        sleep(11);
        $connection->connect();
    };

    // 当连接远程websocket服务器的连接断开时
    $con->onClose = function($connection){
        echo "connection closed and try to reconnect\n";
        // 如果连接断开，1秒后重连
        $connection->reConnect(1);
    };
// 当收到消息时
    $con->onMessage = function (AsyncTcpConnection $con, $data) {
        echo $data;
    };

    $con->connect();
};



Worker::runAll();